﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Library.AppWeb
{
    public class ResultMessage
    {
        private bool bolIsError = false;
        private string strMessage = string.Empty;
        private string strMessageDetail = string.Empty;
        private ErrorTypes enErrorType;

        /// <summary>
        /// Bị lỗi/dữ liệu đã tồn tại/kết quả kiểm tra
        /// </summary>
        public bool IsError
        {
            get { return bolIsError; }
            set { bolIsError = value; }
        }
        
        /// <summary>
        /// Tiêu đề lỗi/kết quả
        /// </summary>
        public string Message
        {
            get { return strMessage; }
            set { strMessage = value; }
        }
        
        /// <summary>
        /// Nội dung lỗi/nội dung kiểm tra
        /// </summary>
        public string MessageDetail
        {
            get { return strMessageDetail; }
            set { strMessageDetail = value; }
        }
        
        /// <summary>
        /// Loại lỗi
        /// </summary>
        public ErrorTypes ErrorType
        {
            get { return enErrorType; }
            set { enErrorType = value; }
        }

        public ResultMessage()
        {
        }

        public ResultMessage(bool bolIsError, ErrorTypes enErrorType, string strMessage, Exception objEx)
        {
            this.bolIsError = false;
            this.strMessage = string.Empty;
            this.strMessageDetail = string.Empty;

            int startIndex=objEx.Message.IndexOf("[+")+2;
            int index = objEx.Message.IndexOf("+]");
            if (startIndex < index)
            {
                strMessage = objEx.Message.Substring(startIndex, index - startIndex);
                enErrorType = ErrorTypes.UserDefine;
                this.strMessageDetail = string.Empty;
            }
            this.bolIsError = bolIsError;
            this.enErrorType = enErrorType;
            this.strMessage = strMessage;
            if (objEx.InnerException != null)
            {
                Exception level1 = objEx.InnerException;
                if (level1.InnerException != null)
                {
                    this.strMessageDetail = level1.InnerException.Message.ToString();
                }
                else
                    this.strMessageDetail = level1.Message.ToString();
            }
            else
                this.strMessageDetail = objEx.Message.ToString();
        }

        public ResultMessage(bool bolIsError, ErrorTypes enErrorType, string strMessage, string strMessageDetail)
        {
            this.bolIsError = false;
            this.strMessage = string.Empty;
            this.strMessageDetail = string.Empty;
            int startIndex = strMessageDetail.IndexOf("[+") + 2;
            int index = strMessageDetail.IndexOf("+]");
            if (startIndex < index)
            {
                strMessage = strMessageDetail.Substring(startIndex, index - startIndex);
                enErrorType = ErrorTypes.UserDefine;
                strMessageDetail = string.Empty;
            }
            this.bolIsError = bolIsError;
            this.enErrorType = enErrorType;
            this.strMessage = strMessage;
            this.strMessageDetail = strMessageDetail;

        }

        public enum ErrorTypes
        {
            No_Error,
            LoadInfo,
            Insert,
            Update,
            Delete,
            SearchData,
            SelectData,
            GetData,            
            TokenNotExist,
            TokenInvalid,
            CheckData,
            Others,
            UserDefine,
            InvalidIV,
            Login,
            Logout
        }
    }
}
